package com.sunshine.wrongquestionset.activity.splash

import android.content.Intent
import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider

import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.activity.login.LoginActivity
import com.sunshine.wrongquestionset.activity.main.MainActivity

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        val viewModel: SplashViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application)).get(SplashViewModel::class.java)

        val hasLoginUserLiveData = viewModel.hasLoginUser()
        hasLoginUserLiveData.observe(this, Observer {
            //没有登录跳转到登录界面
            if (it.isNullOrBlank()) {
                startActivity(Intent(this, LoginActivity::class.java))
                finish()
            }
            //已经登录进入主界面
            else {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            }
            hasLoginUserLiveData.removeObservers(this)
        })
    }
}